package com.huang.builder;

public class BuilderPattern {
    public static void main(String[] args) {
        //创建指挥者对象，在这之前需要创建建造者类，传入指挥者类中
        Builder ofoBuilder = new OfoBuilder();
        Director director = new Director(ofoBuilder);
        //让指挥者指挥组装自行车
        Bike bike = director.construct();
        System.out.println(bike);
    }
}

/**
 * 产品对象
 */
class Bike {
    //本应该是复杂的对象，为了演示方便，只写了两个属性
    private String frame;
    private String seat;

    public String getFrame() {
        return frame;
    }

    public void setFrame(String frame) {
        this.frame = frame;
    }

    public String getSeat() {
        return seat;
    }

    public void setSeat(String seat) {
        this.seat = seat;
    }

    @Override
    public String toString() {
        return "Bike{" +
                "frame='" + frame + '\'' +
                ", seat='" + seat + '\'' +
                '}';
    }
}

/**
 * 抽象建造者类
 */
abstract class Builder {
    //构建自行车的各个组成部分
    public abstract void buildFrame();

    public abstract void buildSeat();

    //构建自行车的方法
    public abstract Bike createBike();
}

/**
 * 具体建造者类，用来构建摩拜单车builder
 */
class MobikeBuilder extends Builder {
    //声明bike类型的变量，用于赋值
    protected Bike bike = new Bike();

    @Override
    public void buildFrame() {
        bike.setFrame("碳纤维车架");
    }

    @Override
    public void buildSeat() {
        bike.setSeat("真皮座椅");
    }

    @Override
    public Bike createBike() {
        return bike;
    }
}

/**
 * 具体建造者类
 */
class OfoBuilder extends Builder {
    //声明bike类型的变量，用于赋值
    protected Bike bike = new Bike();

    @Override
    public void buildFrame() {
        bike.setFrame("路合金车架");
    }

    @Override
    public void buildSeat() {
        bike.setSeat("纤维座椅");
    }

    @Override
    public Bike createBike() {
        return bike;
    }
}

/**
 * 指挥者类
 */
class Director {
    //声明builder类型变量，将builder传入，给director进行按顺序组装
    private Builder builder;

    public Director(Builder builder) {
        this.builder = builder;
    }

    //组装自行车的功能，按照一定的顺序
    public Bike construct() {
        builder.buildFrame();
        builder.buildSeat();
        return builder.createBike();

    }

}



